﻿using coreapi.MdelsWang;
using LinCms.Core.Entities;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using SqlSugar;

namespace coreapi.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v3")]
    public class BeverageInfoController : ControllerBase
    {
        private readonly IFreeSql db;
        public BeverageInfoController(IFreeSql _db)
        {
            this.db = _db;
        }
        [HttpGet]
        public object Get(int currentPage = 1, int pageSize = 10, string? search = "")
        {
            int currentPages = currentPage; // 当前页码
            int pageSizes = pageSize; // 每页显示的条目数
            var query = db.Select<Beverage, BeverageInformation>()
                      .LeftJoin((a, b) => a.BeverageID == b.BeverageID)
                      .WhereIf(!string.IsNullOrEmpty(search), (a, b) => a.BeverageName.Contains(search))
                      .ToList((a, b) => new { a.BeverageID, a.BeverageName, a.BeverageType, a.Price, a.Description, b.ProductionDate, b.ShelfLife });
            int totalCount = (int)query.Count(); // 总条目数
            var list = query.Skip((currentPages - 1) * pageSize).Take(pageSize).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="beverage"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post(BeverageInfo beverage)
        {
            var beverage1 = new Beverage()
            {
                BeverageName = beverage.BeverageName,
                BeverageType = beverage.BeverageType,
                Price = beverage.Price,
                Description = beverage.Description
            };
            db.Insert(beverage1).ExecuteAffrows();

            int maxId = db.Select<Beverage>().Max(b => b.BeverageID);

            var beverage2 = new BeverageInformation()
            {
                BeverageID = maxId,
                ProductionDate = beverage.ProductionDate,
                ShelfLife = beverage.ShelfLife,
            };
            var affectedRows = db.Insert(beverage2).ExecuteAffrows();

            return affectedRows;
        }
        [HttpPut]
        public int Put(string BeverageID, BeverageInfo beverage)
        {
            var beverage1 = db.Update<Beverage>().Set(a => new Beverage
            {
                BeverageName = beverage.BeverageName,
                BeverageType = beverage.BeverageType,
                Price = beverage.Price,
                Description = beverage.Description
            }).Where(a => a.BeverageID == int.Parse(BeverageID)).ExecuteAffrows();

            var beverage2 = db.Update<BeverageInformation>().Set(a => new BeverageInformation
            {
                BeverageID = int.Parse(BeverageID),
                ProductionDate = beverage.ProductionDate,
                ShelfLife = beverage.ShelfLife,
            }).Where(a => a.InformationID == int.Parse(BeverageID)).ExecuteAffrows();

            return beverage2;
        }
        [HttpDelete]
        public int Delete(int BeverageID)
        {
            var beverage2 = db.Delete<BeverageInformation>()
                                .Where(a => a.InformationID == BeverageID)
                                .ExecuteAffrows();
            var beverage1 = db.Delete<Beverage>()
                                .Where(a => a.BeverageID == BeverageID)
                                .ExecuteAffrows();
          

            return beverage1;
        }
    }
}
